package light.head.dao.oa;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import light.head.cache.Cache;
import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.model.Vehicle;

import org.springframework.stereotype.Service;

@Service
public class VehicleDAO extends BaseDAO<Vehicle, Integer>{
	
	private final static String sql_loadAll = "FROM Vehicle";
	private final static String sql_findByPlate = "FROM Vehicle v WHERE v.plate = ?";
	
	public RESULT findByMap(Map<String, String> map, PageBean pageBean) {
		StringBuffer hql = new StringBuffer("FROM Vehicle v WHERE 1 = 1");
		
		List<Object> list = null;
		if (map != null) {
			list = new ArrayList<Object>();
			String name = map.get(Parm.NAME);			//车辆名称
			String plate = map.get(Parm.PLATE);			//车牌号
			String status = map.get(Parm.STATUS);		//状态
			String oc = map.get(Parm.OC);
			if (null != name && !"".equals(name)) {
				hql.append(" AND v.name LIKE ?");
				list.add("%" + name + "%");
			}if(isNotNull(plate)){
				hql.append(" AND v.plate LIKE ?");
				list.add("%" + plate + "%");
			}if(isNotNull(status)){
				hql.append(" AND v.status = ?");
				list.add(Byte.parseByte(status));
			}if(isNotNull(oc)){
				hql.append(" AND v.oc = ?");
				list.add(Integer.parseInt(oc));
			}
			
		}
		
		hql.append(" ORDER BY v.code DESC");
		
		if (findToPageBean(hql.toString(), pageBean, list) == RESULT.SUCCESS)
			return RESULT.SUCCESS;
		return RESULT.ERROR;
	}
	
	
	public RESULT findByMapOc(Integer oc,Map<String, String> map, PageBean pb) {
		StringBuffer hql = new StringBuffer(" FROM Vehicle v WHERE v.oc = ?");
		List<Object> list = new ArrayList<Object>();
		list.add(oc);
		if (map != null) {
			String name = map.get(Parm.NAME);			//车辆名称
			String plate = map.get(Parm.PLATE);			//车牌号
			String status = map.get(Parm.STATUS);		//状态
			if (null != name && !"".equals(name)) {
				hql.append(" AND v.name LIKE ?");
				list.add("%" + name + "%");
			}if(isNotNull(plate)){
				hql.append(" AND v.plate LIKE ?");
				list.add("%" + plate + "%");
			}if(isNotNull(status)){
				hql.append(" AND v.status = ?");
				list.add(Byte.parseByte(status));
			}
		}
		
		hql.append(" ORDER BY v.code DESC");
		return findToPageBean(hql.toString(), pb, list);
	}
	
	/**
	 * 修改车辆信息
	 */
	public RESULT update(Vehicle vehicle) {
		if (null == vehicle || null == vehicle.getCode())
			return RESULT.PARAMETER_ERROR;
		
		Vehicle old = get(vehicle.getCode());
		if (null == old)
			return RESULT.NOT_FIND;
		if(null !=(vehicle.getName())) {
			old.setName(vehicle.getName());
		}if(null !=(vehicle.getStatus())){
			old.setStatus(vehicle.getStatus());
		}if (RESULT.SUCCESS == upd(old)) {
			return RESULT.SUCCESS;
		} else {
			return RESULT.ERROR;
		}
	}
	
	
	public Vehicle findByPlate(String plate) {
		List<Vehicle> v = find(sql_findByPlate,plate);
		return v.get(0);
	}
	public List<Vehicle> getAll() {
		if (null == Cache.vehicle || Cache.vehicle.size() == 0)
			return loadAll();
		return Cache.vehicle;
	}
	
	public List<Vehicle> loadAll() {
		Cache.vehicle = find(sql_loadAll);
		return Cache.vehicle;
	}
	/**
	 * 根据拼接好的车辆编号序列获取对应的车辆列表
	 */
	public List<Vehicle> findUserByCodes(String codes) {
		if (!isNotNull(codes)) {
			return null;
		}
		String sql = "FROM Vehicle u WHERE u.deleted = 0 AND u.deleted IS NOT NULL AND u.code IN (" + codes + ")";
		List<Vehicle> list = find(sql);
		return list;
	}
	
	/**
	 * 根据条件分页查询车辆信息
	 * @param	查询条件
	 * @param	分页对象
	 */
	public RESULT findSub(List<Integer> ocs, Map<String, String> map, PageBean pageBean) {
		StringBuffer hql = new StringBuffer("FROM Vehicle u WHERE u.deleted = 0 AND u.oc IN(");
		List<Object> list =  new ArrayList<Object>();
		for (Integer i : ocs) {
			hql.append("?,");
			list.add(i);
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		if (map != null) {
			String realName = map.get(Parm.REAL_NAME);
			String oc = map.get(Parm.OC);
			String suc = map.get(Parm.SUC);

			if (isNotNull(realName)) {
				hql.append(" AND u.name LIKE ?");
				list.add("%" + realName + "%");
			}
			if (isNotNull(oc)) {
				hql.append(" AND u.oc = ?");
				list.add(Integer.parseInt(oc));
			}
			if (isNotNull(suc)) {
				hql.append(" AND u.code NOT IN (" + suc + ")");
			}
		}
		hql.append(" ORDER BY u.oc, u.code DESC");
		
		return findToPageBean(hql.toString(), pageBean, list);
	}
	
	
	/**
	 * 通过车辆类型查询所有未使用 的车辆信息
	 * @param oc	机构编号
	 * @param vtc	类型编号
	 */
	public List<Vehicle> findByVtc(Integer oc, Integer vtc) {
		if (null == oc || null == vtc) return null;
		StringBuffer hql = new StringBuffer("from Vehicle v WHERE v.vtc = ? AND v.oc = ? AND v.status = 2");
		return find(hql.toString(), null, null, vtc, oc);
	}
	
	public List<Vehicle> findPlate(String plate) {
		List<Vehicle> v = find(sql_findByPlate,plate);
		return v;
	}
}
